package com.xixibaby.core.message;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.xixibaby.core.SpringUtil;
import com.xixibaby.core.SuperMq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.ByteUtils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

@Slf4j
public class AsynExcuteHandler implements AsynExcute<ExeObj> {

    @Override
    public void excute(Integer time, Channel<ExeObj> channel, ExeObj obj) {
        SuperMq bean = SpringUtil.getBean(SuperMq.class);
        MqMessage message = new MqMessage();
        message.setCode(channel.code());
        message.setObject(obj);
        message.setChannel(channel);
        message.setTarget(obj.getTarget());
        assert bean != null;

        ByteArrayOutputStream p = new ByteArrayOutputStream();
        IoUtil.writeObj(p,true,message);
        byte[] bytes = p.toByteArray();
        bean.sendMsgToMq(bytes, time);
    }

    @Override
    public boolean support() {
        SuperMq bean = SpringUtil.getBean(SuperMq.class);
        return null != bean;
    }
}
